package com.wwj.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.reggie.common.CustomException;
import com.wwj.reggie.entity.Category;
import com.wwj.reggie.entity.Dish;
import com.wwj.reggie.entity.Setmeal;
import com.wwj.reggie.mapper.CategoryMapper;
import com.wwj.reggie.service.CategoryService;
import com.wwj.reggie.service.DishService;
import com.wwj.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long ids) {
        LambdaQueryWrapper<Dish> lqw_dish = new LambdaQueryWrapper<>();
        //添加查询条件：根据id进行查询
        lqw_dish.eq(Dish::getCategoryId, ids);
        int count1 = dishService.count(lqw_dish);

        //查询当前分类是否关联菜品
        if (count1 > 0){
            throw new CustomException("当前分类关联了菜品，不能删除！！！");
        }

        //查询当前分类是否关联套餐
        LambdaQueryWrapper<Setmeal> lqw_setMeal = new LambdaQueryWrapper<>();
        //添加查询条件：根据id进行查询
        lqw_setMeal.eq(Setmeal::getCategoryId, ids);
        int count2 = setmealService.count(lqw_setMeal);

        if (count2 > 0){
            throw new CustomException("当前分类关联了套餐，不能删除！！！");
        }

        //正常删除分类
        super.removeById(ids);
    }
}
